﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using cognysClient.CognyService;

namespace cognysClient
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        //gets all questions from them QoR table, and return them as an array.
        [WebMethod]
        public static string[] getQorQuestions()
        {
            var client = new Service1Client();
            List<QOR> qors = new List<QOR>();
            qors = client.getQorQuestions().ToList();

            List<string> list = new List<string>();

            foreach (var qor in qors)
            {
                list.Add(qor.Question);
            }
            String[] qorQuestions = list.ToArray();
            return qorQuestions;

        }

        //saves the Qor test to the resultQoR table.
        [WebMethod]
        public static void SaveResult(string[] answers)
        {
            var client = new Service1Client();

            List<QOR> qors = new List<QOR>();

            DateTime regDate = DateTime.Now;
            qors = client.getQorQuestions().ToList();  

            int index = 0;
            
            foreach (var answer in answers)
            {
                ResultQoR result = new ResultQoR();
                result.Person = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());
                result.Answer = Int32.Parse(answer);
                result.Question = qors[index].ID;
                result.regDate = regDate.ToString();
                index++;

                client.saveQor(result); 
            }

        }

    }

}